#include <string>
#include <algorithm>
using namespace std;
class Solution {
public:
    string addBinary(string a, string b) {
        int n=a.size()-1, m=b.size()-1, g=0;
        string ret="";
        while (n>=0 || m>=0 || g>0) {
            g+=((n>=0)?(a[n--]-'0'):0)+((m>=0)?(b[m--]-'0'):0);
            ret.push_back((char)(g%2+'0'));
            g/=2;
        }
        for (int i=0;i<ret.size()/2;i++) swap(ret[i], ret[ret.size()-1-i]);
        return ret;
    }
};
